<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2017/6/3
 * Time: 14:15
 */
class PermissionService extends BaseService
{
    protected $user;
    private $userService;
    private $diaryListCacheModel;

    function __construct()
    {
        $this->userService          =   new UserService();
        $this->diaryListCacheModel  =   new DiaryListCacheModel();
    }

    /**
     * 检测用户是否被封禁
     * @param $uid
     * @return bool(是|true, 否|false)
     */
    public function checkUserIsBlocked($uid)
    {
        $result = $this->userService->checkUserStatus($uid);
        return $result ? false : true;
    }

    /**
     * 检测host_uid用户是否被uid拉黑
     * @param $uid
     * @return bool(是|true, 否|false)
     */
    public function checkUserIsBlacked($uid, $host_uid)
    {
        $result = $this->userService->checkBlackStatus($uid, $host_uid);
        if (empty($result)) {
            $this->errorNo    =   'PMS001';
            $this->errorMsg   =   '你已被对方拉黑，无法进行该操作';
        }
        return $result ? false : true;
    }

    /**
     * 检测用户评论是否重复
     * @param $uid
     * @param $content
     * @return bool(是|true, 否|false)
     */
    public function checkCommentRepateContent($uid, $content)
    {
        $ck = md5($content);
        $lastCk = $this->diaryListCacheModel->getLastCheckCommentConnent($uid);
        if($lastCk && $lastCk==$ck) {
            $this->errorNo    =   'PMS002';
            $this->errorMsg   =   '点滴评论内容重复提交';
            return  true;
        }

        return false;
    }

    /**
     * 验证uid是否和HOST_UID相同
     * @param $uid
     * @return bool(是|true, 否|false)
     */
    public function checkUserIsHostUser($uid)
    {
        if(!$uid){
            AC::$errorNo    =   'UC010';
            return  false;
        }
        if (AC::HOST_UID != $uid){
            AC::$errorNo    =   'UC010';
            return  false;
        }
        return true;
    }

    /**
     * 验证评论权限（是否被屏蔽）（true/可评论，false/不可评论）
     * @param $uid
     * @return bool
     */
    public function checkShieldUser($uid)
    {
        if(!$uid){
            $this->errorNo    =   'UC010';
            return  false;
        }
        $host_uid       = AC::HOST_UID;
        if ($host_uid == $uid){
            return  true;
        }

        $time = AC::SERVER_TIME;
        $lasTime = $time - AC::ONEDAY;

        //清除过期屏蔽用户列表
        $result = $this->diaryListCacheModel->removeUserShieldListItemByScore($uid, AC::ONEDAY, $lasTime);
        //获取列表
        $userShieldList = $this->diaryListCacheModel->getUserShieldList($uid, true);
        if($result) {
            //更改数据库
            $setInfo = [
                'extra' => json_encode($userShieldList)
            ];
            $userSettingModel = new UserSettingModel();
            $userSettingModel->saveSetting($uid, $setInfo);
        }
        if(array_key_exists($host_uid, $userShieldList)){
            $userInfo = $this->userService->getUserInfo($uid, $host_uid);
            //如果在屏蔽列表中则拒绝评论
            $this->errorNo    = 'DCC010';
            $this->errorMsg   = '你已被' . $userInfo['nickname'] . '屏蔽，24小时内不可回复此点滴';
            return false;
        }
        return true;
    }

}